package com.pentalog.ginet;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.pentalog.ginet.domain.VModule;
import com.pentalog.util.HibernateUtil;

/**
 * Hello world!
 *
 */
public class TestVModule 
{
	public static void main( String[] args ){
		TestVModule test = new TestVModule();
//		test.save();
		test.getAndUpdate();
	}
	
    public void save()
    {
    	Session session = null;
		Transaction tx = null;
		try {
			
			session = HibernateUtil.getSessionFactory().openSession();			
			tx = session.beginTransaction();
			VModule mod = new VModule();
	    	mod.setOwner("John Smith");
	    	mod.setName("Module 1");
	    	session.save(mod);
	    	session.flush();
	    	System.out.println(mod.getVersion());
	    	mod.setOwner("James Doe");
	    	session.save(mod);
	    	session.flush();
	    	System.out.println(mod.getVersion());
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
    }
    public void getAndUpdate()
    {
    	Session session = null;
		Transaction tx = null;
		try {
			
			session = HibernateUtil.getSessionFactory().openSession();			
			tx = session.beginTransaction();
			VModule v1 = (VModule)session.get(VModule.class, 1);
	    	session.flush();
	    	System.out.println(v1.getVersion());
	    	v1.setOwner("accs");
	    	session.save(v1);
	    	session.flush();
	    	System.out.println(v1.getVersion());
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
    }
}
